package com.qdairlines.controller.file;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.qdairlines.common.ControllerUtil;
import com.qdairlines.constant.BaseConsts;
import com.qdairlines.controller.base.BaseController;
import com.qdairlines.entity.file.FastDFSFile;
import com.qdairlines.service.file.FastDFSFileService;
import com.qdairlines.service.file.FastDFSFileService.DELETE_TYPE;

/**
 * 
 * @author pangyannan
 * @date 2017年4月14日
 * @company QingDao Airlines
 * @description 附件上传控制器
 */
@Controller
@RequestMapping("file")
public class FileController extends BaseController{
	
	@Autowired
	private FastDFSFileService fastDFSFileService;
    /**
     * 向FastDFS上传文件，并保存到数据库中上传的信息  by庞衍楠20161215
     * @param fileData
     * @param type
     * @param typeKey
     * @param request
     * @return 上传成功后的id
     */
    @RequestMapping(value="upload", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String upload(@RequestParam MultipartFile fileData, @RequestParam String type,@RequestParam String typeKey,HttpServletRequest request){
    	String fastdfsFilesId = fastDFSFileService.add(ControllerUtil.getUser(request).getWorkcode(), type, typeKey, fileData);
    	if("0".equals(fastdfsFilesId)){
    		//上传失败
    		return 	 JSONObject.fromObject(BaseConsts.RESPONSE_MAP_ERROR).toString();
    	}else{
    		//上传成功
    		return 	 JSONObject.fromObject(BaseConsts.RESPONSE_MAP_SUCCESS).toString();
    	}  
    }
    /**
     * 下载文件，如文件未找到或者没有权限，则返回null
     * @param id
     * @return
     */
   @RequestMapping(value="download", produces = "text/html;charset=utf-8")
   @ResponseBody
   public ResponseEntity<byte[]> download(String id){
	   return   fastDFSFileService.downloadFileById(id);  
   }
   
   
   /**
    * 逻辑删除，置标志位为删除标记，文件依然放在服务器上不动
    * @param id
    * @return
    */
   @RequestMapping(value="deleteLogic", produces = "text/html;charset=utf-8")
   @ResponseBody
   public String deleteLogic(String id,HttpServletRequest request){
	  int flag = fastDFSFileService.deleteById(ControllerUtil.getUser(request).getWorkcode(), id, DELETE_TYPE.LOGIC);
	  if(flag == 0){
   		//删除失败
			return 	 JSONObject.fromObject(BaseConsts.RESPONSE_MAP_ERROR).toString();
	  }else{
	   	//删除成功
		  return 	 JSONObject.fromObject(BaseConsts.RESPONSE_MAP_SUCCESS).toString();
	  } 
   }
   
   /**
    * 物理删除，刪除数据库中数据，删除文件服务器文件
    * @param id
    * @return
    */
   @RequestMapping(value="deleteTrue", produces = "text/html;charset=utf-8")
   @ResponseBody
   public String deleteTrue(String id,HttpServletRequest request){
	  int flag = fastDFSFileService.deleteById(ControllerUtil.getUser(request).getWorkcode(), id, DELETE_TYPE.TRUE);
	  if(flag == 0){
		//删除失败
		  return 	 JSONObject.fromObject(BaseConsts.RESPONSE_MAP_ERROR).toString();
	  }else{
		//删除成功
		  return 	 JSONObject.fromObject(BaseConsts.RESPONSE_MAP_SUCCESS).toString();
	  } 
   }
   
   /**
    * 获取文件list
    * @param request
    * @return
    */
   @RequestMapping("getFilesList")
   @ResponseBody
   public String getFilesListFastDFS(HttpServletRequest request,String type,String typeKey) {
	List<FastDFSFile> uploadsFileList = fastDFSFileService.getByTypeId(type, typeKey);
	return ControllerUtil.getJson(uploadsFileList);
   }
   
	/**
	 * 缩略图下载
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("getSmallImage")
	@ResponseBody
	public ResponseEntity<byte[]> getSmallImage(String id,Integer maxLength){
		return fastDFSFileService.getSmallImage(id, maxLength);
	}
   
}
